class Solution {
    public int[][] matrixReshape(int[][] mat, int r, int c) {
        int rr = mat.length;
        int cc = mat[0].length;
        if(rr*cc != r*c){
            return mat;
        }
        int[][] results = new int[r][c];
        int m = 0;
        int n = 0;
        for(int i = 0; i < rr; i++){
            for(int j = 0; j < cc; j++){
                if(n<c){
                    results[m][n++] = mat[i][j];
                }else{
                    n = 0;
                    m += 1;
                    results[m][n++] = mat[i][j];
                }
                
            }

        }
        return results;
    }
}
